package Demo3;

import java.util.Arrays;
import java.util.Objects;

public class Test {
    public static void main(String[] args) {
        Person person1 = new Person("zhangsan",10);
        Person person2 = new Person("zhangsan",10);
        Person person3 = new Person();
        person3 = person1;
        //两个一样的对象我们想放在相同的位置    此时需要重写hasCode方法
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
        //重写toString
        System.out.println(person1);
        System.out.println(person2);
        //此时比较的是地址
        System.out.println(person1 == person2);
        //比较的是对象的内容是否相同   需要重写equals方法
        System.out.println(person1.equals(person2));
        System.out.println(person3.equals(person1));
    }
}
class Person{
    public String name;
    public int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
